package cn.edu.cqupt.cxcy.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqupt.cxcy.dto.AdminImpl;
import cn.edu.cqupt.cxcy.dto.IndexImpl;
import cn.edu.cqupt.cxcy.util.Tools;
import cn.edu.cqupt.cxcy.util.Validate;

/**
 * 主页-【创业辅导】
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class GetbPassInfo extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String status = request.getParameter("status");//1：导师；2：团队
		String loname = request.getParameter("loname");
		String password = Tools.getMD5(Tools.getMD5(request.getParameter("password")));
		String code = request.getParameter("code");
		System.out.println(status  + "," + loname + "," + password + "," + code + ",");
		if( !Validate.checkEmail(loname) || new IndexImpl().usernameIsUnique(loname) || password.equals(Tools.getMD5(Tools.getMD5("")))) {
			resp.sendRedirect(request.getHeader("referer"));
			return;
		}
		if (!((String)request.getSession().getAttribute("safecode")).equalsIgnoreCase(code)) { //验证验证码
			resp.sendRedirect(request.getHeader("referer"));
			return;
		}
		if(!status.equals(new AdminImpl().getStatus(loname))) {
			resp.sendRedirect(request.getHeader("referer"));
			return;
		}
		if(new AdminImpl().updatePass(loname, password)) {
			resp.sendRedirect("index");
		} else {
			resp.sendRedirect(request.getHeader("referer"));
			return;
		}
	}

}
